#ifndef U2StringUtil_H
#define U2StringUtil_H


#include "U2PreRequest.h"
#include "U2SharedPtr.h"


// If we're using the GCC 3.1 C++ Std lib
#if U2_COMPILER == U2_COMPILER_GNUC && U2_COMP_VER >= 310 && !defined(STLPORT)
// <ext/hash_map>
// For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
#   if U2_COMP_VER < 430
		namespace __gnu_cxx
		{
			template <> struct hash< U2EG_NAMESPACE_NAME::_U2StringBase >
			{
				size_t operator()( const U2EG_NAMESPACE_NAME::_U2StringBase _stringBase ) const
				{
					/* This is the PRO-STL way, but it seems to cause problems with VC7.1
					   and in some other cases (although I can't recreate it)
					hash<const char*> H;
					return H(_stringBase.c_str());
					*/
					/** This is our custom way */
					register size_t ret = 0;
					for( U2EG_NAMESPACE_NAME::_U2StringBase::const_iterator it = _stringBase.begin(); it != _stringBase.end(); ++it )
                    {
						ret = 5 * ret + *it;
                    }

					return ret;
				}
			};
		}
#   endif
#endif




U2EG_NAMESPACE_BEGIN


typedef std::vector<U2String>			U2StringVector;
typedef U2SharedPtr<U2StringVector>		U2StringVectorPtr;


class _U2Share U2StringUtil
{
public:
	typedef std::ostringstream		U2StrStreamType;

    /** Removes any whitespace characters, be it standard space or
        TABs and so on.
    @remarks
        The user may specify wether they want to trim only the
        beginning or the end of the U2String ( the default action is
        to trim both).
    */
    static void trim( U2String& str, bool left = true, bool right = true );

    /** Returns a U2StringVector that contains all the substrings delimited
        by the characters in the passed <code>delims</code> argument.
    @param
        delims A list of delimiter characters to split by
    @param
        maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
        parameters is > 0, the splitting process will stop after this many splits, left to right.
    @param
        preserveDelims Flag to determine if delimiters should be saved as substrings
    */
    static U2StringVector split( const U2String& str, const U2String& delims = "\t\n ", unsigned int maxSplits = 0, bool preserveDelims = false);

	/** Returns a U2StringVector that contains all the substrings delimited
        by the characters in the passed <code>delims</code> argument, 
		or in the <code>doubleDelims</code> argument, which is used to include (normal) 
		delimeters in the tokenised string. For example, "strings like this".
    @param
        delims A list of delimiter characters to split by
	@param
        delims A list of double delimeters characters to tokenise by
    @param
        maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
        parameters is > 0, the splitting process will stop after this many splits, left to right.
    */
    static U2StringVector tokenise( const U2String& str, const U2String& delims = "\t\n ", const U2String& doubleDelims = "\"", unsigned int maxSplits = 0);

	/** Lower-cases all the characters in the string.
    */
    static void toLowerCase( U2String& str );

    /** Upper-cases all the characters in the string.
    */
    static void toUpperCase( U2String& str );

    /** Returns whether the string begins with the pattern passed in.
    @param pattern The pattern to compare with.
    @param lowerCase If true, the start of the string will be lower cased before
        comparison, pattern should also be in lower case.
    */
    static bool startsWith(const U2String& str, const U2String& pattern, bool lowerCase = true);

    /** Returns whether the string ends with the pattern passed in.
    @param pattern The pattern to compare with.
    @param lowerCase If true, the end of the string will be lower cased before
        comparison, pattern should also be in lower case.
    */
    static bool endsWith(const U2String& str, const U2String& pattern, bool lowerCase = true);

    /** Method for standardising paths - use forward slashes only, end with slash.
    */
    static U2String standardisePath( const U2String &init);

	/** Returns a normalized version of a file path
	This method can be used to make file path strings which point to the same directory  
	but have different texts to be normalized to the same text. The function:
	- Transforms all backward slashes to forward slashes.
	- Removes repeating slashes.
	- Removes initial slashes from the beginning of the path.
	- Removes ".\" and "..\" meta directories.
	- Sets all characters to lowercase (if requested)
	@param init The file path to normalize.
	@param makeLowerCase If true, transforms all characters in the string to lowercase.
	*/
   static U2String normalizeFilePath(const U2String& init, bool makeLowerCase = true);


    /** Method for splitting a fully qualified filename into the base name
        and path.
    @remarks
        Path is standardised as in standardisePath
    */
    static void splitFilename(const U2String& qualifiedName,
        U2String& outBasename, U2String& outPath);

	/** Method for splitting a fully qualified filename into the base name,
	extension and path.
	@remarks
	Path is standardised as in standardisePath
	*/
	static void splitFullFilename(const U2String& qualifiedName, 
		U2String& outBasename, U2String& outExtention, 
		U2String& outPath);

	/** Method for splitting a filename into the base name
	and extension.
	*/
	static void splitBaseFilename(const U2String& fullName, 
		U2String& outBasename, U2String& outExtention);

    /** Simple pattern-matching routine allowing a wildcard pattern.
    @param str U2String to test
    @param pattern Pattern to match against; can include simple '*' wildcards
    @param caseSensitive Whether the match is case sensitive or not
    */
    static bool match(const U2String& str, const U2String& pattern, bool caseSensitive = true);

	/** replace all instances of a sub-string with a another sub-string.
	@param source Source string
	@param replaceWhat Sub-string to find and replace
	@param replaceWithWhat Sub-string to replace with (the new sub-string)
	@returns An updated string with the sub-string replaced
	*/
	static const U2String replaceAll(const U2String& source, const U2String& replaceWhat, const U2String& replaceWithWhat);

    /// Constant blank string, useful for returning by ref where local does not exist
    static const U2String BLANK;

};


#if U2_PLATFORM != U2_PLATFORM_ANDROID

#if U2_COMPILER == U2_COMPILER_GNUC && U2_COMP_VER >= 310 && !defined(STLPORT)
#   if U2_COMP_VER < 430
typedef ::__gnu_cxx::hash< _U2StringBase >		_U2StringHash;
#   else
typedef ::std::tr1::hash< _U2StringBase >		_U2StringHash;
#   endif
#elif U2_COMPILER == U2_COMPILER_MSVC && U2_COMP_VER >= 1600 && !defined(STLPORT) // VC++ 10.0
typedef ::std::tr1::hash< _U2StringBase >									_U2StringHash;
#elif !defined( _STLP_HASH_FUN_H )
typedef stdext::hash_compare< _U2StringBase, std::less< _U2StringBase > >	_U2StringHash;
#else
typedef std::hash< _U2StringBase >											_U2StringHash;
#endif

#endif



U2EG_NAMESPACE_END

#endif